package com.sunlands.zlcx.usercenter.config;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.sunlands.zlcx.usercenter.interceptor.SignInterceptor;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.AsyncTaskExecutor;
import org.springframework.http.HttpHeaders;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

/**
 * <p>webmvc配置</p>
 * <PRE>
 * <BR>    修改记录
 * <BR>-----------------------------------------------
 * <BR>    修改日期         修改人          修改内容
 * </PRE>
 *
 * @author zl
 * @version 1.0
 * @package: com.sunlands.zlcx.mpmsgservice.config
 * @date Created in 2017/11/30 15:41
 * @copyright: Copyright (c) founders
 */
@Configuration
public class WebAppConfigurer extends WebMvcConfigurerAdapter {

    @Autowired
    SignInterceptor signInterceptor;

    @Bean
    ObjectMapper objectMapper() {
        ObjectMapper objectMapper = new ObjectMapper();
        //objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);

        return objectMapper;
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(signInterceptor).addPathPatterns("/**");

        super.addInterceptors(registry);
    }

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("*")
                .allowedMethods("*")
                .allowedHeaders("*")
                .allowCredentials(true)
                .exposedHeaders(HttpHeaders.SET_COOKIE).maxAge(3600L);
    }



    public static final long timeout = 5 * 1000;

    public enum WebTimeOutEnum {
        DOWN_ASSERTS_LIABILITY("/upload/avatar-url", 20 * 1000),
        ;

        private String serviceUrl;
        private long timeout;

        WebTimeOutEnum(String serviceUrl, long timeout) {
            this.serviceUrl = serviceUrl;
            this.timeout = timeout;
        }

        public static WebTimeOutEnum url(String url) {
            if(StringUtils.isEmpty(url)){
                return null;
            }
            for (WebTimeOutEnum timeOutEnum : WebTimeOutEnum.values()) {
                if (timeOutEnum.getServiceUrl().equals(url)) {
                    return timeOutEnum;
                }
            }
            return null;
        }

        public String getServiceUrl() {
            return serviceUrl;
        }

        public long getTimeout() {
            return timeout;
        }
    }

}
